﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Skywave.Data
{
    [DataContract]
    public abstract class ChartDataGeneratorAxis : IEqualityComparer<ChartDataGeneratorAxis>, IEquatable<ChartDataGeneratorAxis>, ICloneable
    {

        private string _Id;
        /// <summary>
        /// Id Property
        /// </summary>
        [DataMember]
        public string Id
        {
            get { return _Id; }
            set { _Id = value; }
        }

        private string _Text;
        /// <summary>
        /// Text Property
        /// </summary>
        [DataMember]
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }

        private string _PointTextSeperator = " - ";
        /// <summary>
        /// PointTextSeperator Property
        /// </summary>
        [DataMember]
        public string PointTextSeperator
        {
            get { return _PointTextSeperator; }
            set { _PointTextSeperator = value; }
        }

        private Dictionary<string, string> _JoinClauses = new Dictionary<string, string>();
        /// <summary>
        /// Key: table name used in sql, Value: SQL join clause
        /// </summary>
        [DataMember]
        public Dictionary<string, string> JoinClauses
        {
            get { return _JoinClauses; }
            set { _JoinClauses = value; }
        }

        private Dictionary<string, string> _Params = new Dictionary<string, string>();
        /// <summary>
        /// Key: param name used in declaration, Value: SQL param declaration
        /// </summary>
        [DataMember]
        public Dictionary<string, string> Params
        {
            get { return _Params; }
            set { _Params = value; }
        }

        private int? _MaxCaseCount = 30;
        /// <summary>
        /// MaxCaseCount Property
        /// </summary>
        public int? MaxCaseCount
        {
            get { return _MaxCaseCount; }
            set { _MaxCaseCount = value; }
        }

        public abstract ChartDataGeneratorAxisCases GetCases(string prefixForNames, ChartDataGenerator parent);
        public abstract void CopySettingsFrom(ChartDataGeneratorAxis other);

        public virtual void CopyTo(ChartDataGeneratorAxis target)
        {
            if (target == null)
                return;
            target.Id = this.Id;
            target.Text = this.Text;
            target.JoinClauses = (this.JoinClauses == null) ? null : this.JoinClauses.ToDictionary<KeyValuePair<string, string>, string, string>(q => q.Key, q => q.Value);
            target.Params = (this.Params == null) ? null : this.Params.ToDictionary<KeyValuePair<string, string>, string, string>(q => q.Key, q => q.Value);
        }

        public virtual string GetPointText(List<object> pointValues)
        {
            return new DataStructure.ListHelper<object>(pointValues).ToSeperated(PointTextSeperator);
        }

        public bool Equals(ChartDataGeneratorAxis x, ChartDataGeneratorAxis y)
        {
            if (x == null && y == null)
                return true;
            else if (x == null || y == null)
                return false;
            else
                return string.Equals(x.Id, y.Id);
        }

        public int GetHashCode(ChartDataGeneratorAxis obj)
        {
            return obj.Id.GetHashCode();
        }

        public bool Equals(ChartDataGeneratorAxis other)
        {
            if (other == null)
                return false;
            else
                return string.Equals(Id, other.Id);
        }

        public abstract object Clone();
    }
}
